package cn.cloudreal.cbms.system.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import cn.cloudreal.cbms.system.dao.OrganizaDao;
import cn.cloudreal.cbms.vo.Organiza;

/**
 * <p>
 * Title:CBMS
 * </p>
 * <p>
 * Description:组织架构查询数据Dao层实现类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * <p>
 * Company:云恒瑞通
 * </p>
 * 
 * @author gaoxuejiao
 * @date 2012-07-16
 */
public class OrganizaDaoImpl extends SqlMapClientDaoSupport implements
        OrganizaDao
{

    /** 日志工具类实例 */
    private static final Logger log = Logger.getLogger(OrganizaDaoImpl.class);

    // 组织架构列表
    private static final String GET_ORG_LIST = "organiza.getAllOrganiza";

    // 修改组织架构信息
    private static final String UPDATE_ORG = "organiza.updateOrganiza";

    // 删除组织架构信息
    private static final String DEL_ORG = "organiza.deleteOrganiza";

    // 查询单个权限信息
    private static final String GET_ORG_CODE = "organiza.getOrganizaByCode";

    // 查询单个组织架构信息
    private static final String GET_ORG = "organiza.getOrganiza";

    // 查询是否有用户正在使用
    private static final String IS_USE_BY_USER = "organiza.isUseOrgByUser";

    // 新增组织架构信息
    private static final String ADD_ORG = "organiza.insertOrganiza";

    // 判断当前节点是否是父节点
    private static final String IS_PARENT = "organiza.isParentOrganiza";

    /**
     * 新增
     */
    public int addOrganiza(Organiza organiza)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter OrganizaDaoImpl addOrganiza() method");
        }

        // 执行插入
        Integer result = (Integer) this.getSqlMapClientTemplate()
                .insert(ADD_ORG, organiza);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit OrganizaDaoImpl addOrganiza() method");
        }
        if (null != result)
        {
            return result.intValue();
        }
        return 0;
    }

    /**
     * 删除
     */
    public void delOrganiza(Organiza organiza)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter OrganizaDaoImpl delOrganiza() method");
        }

        // 执行删除
        this.getSqlMapClientTemplate().delete(DEL_ORG, organiza);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit OrganizaDaoImpl delOrganiza() method");
        }
    }

    /**
     * 查询集合
     * 
     * @return boolean true 是父节点 false 不是父节点
     */
    public boolean isParentOrg(String id)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter OrganizaDaoImpl delOrganiza() method");
        }
        Integer count = (Integer) this.getSqlMapClientTemplate()
                .queryForObject(IS_PARENT, id);

        // 如果不存在 则不是 父节点
        if (null == count || count.intValue() <= 0)
        {
            return false;
        }
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit OrganizaDaoImpl delOrganiza() method");
        }
        return true;
    }

    /**
     * 查询是否正在使用
     * 
     * @return boolean true
     */
    public boolean isUseByUser(String id)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter OrganizaDaoImpl delOrganiza() method");
        }
        Integer count = (Integer) this.getSqlMapClientTemplate()
                .queryForObject(IS_USE_BY_USER, id);

        // 如果不存在 则不是 父节点
        if (null == count || count.intValue() <= 0)
        {
            return false;
        }
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit OrganizaDaoImpl delOrganiza() method");
        }
        return true;
    }

    /**
     * 根据id 查询对象信息
     */
    public Organiza getOrganiza(Integer id)
    {
        Organiza org = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter OrganizaDaoImpl getOrganiza() method");
        }
        org = new Organiza();

        // 执行单条记录查询
        org = (Organiza) getSqlMapClientTemplate().queryForObject(GET_ORG, id);

        /**
         * 如果存在父节点在获取
         */
        if (null != org.getParentOrgCode()
                && !org.getParentOrgCode().equals("-1"))
        {
            Organiza temp = new Organiza();
            temp.setOrgCode(org.getParentOrgCode());
            List<Organiza> orglist = getSqlMapClientTemplate().queryForList(GET_ORG_LIST,
                    temp);
            if (null != orglist && orglist.size() > 0)
            {
                temp = orglist.get(0);
            }
            org.setParentOrgName(temp.getOrgName());
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit OrganizaDaoImpl getOrganiza() method");
        }
        return org;
    }

    /**
     * 根据 code 查询对象信息
     */
    public Organiza getOrganizaByCode(String id)
    {
        Organiza org = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter OrganizaDaoImpl getOrganiza() method");
        }
        org = new Organiza();

        // 执行单条记录查询
        org = (Organiza) getSqlMapClientTemplate().queryForObject(GET_ORG_CODE,
                id);

        if (null != org)
        {
            /**
             * 如果存在父节点在获取
             */
            if (null != org.getParentOrgCode()
                    && !org.getParentOrgCode().equals("-1"))
            {
                Organiza temp = new Organiza();
                temp.setOrgCode(org.getParentOrgCode());
                List<Organiza> orglist = getSqlMapClientTemplate().queryForList(GET_ORG_LIST,
                        temp);
                if (null != orglist && orglist.size() > 0)
                {
                    temp = orglist.get(0);
                }
                org.setParentOrgName(temp.getOrgName());
            }
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit OrganizaDaoImpl getOrganiza() method");
        }
        return org;
    }

    /**
     * 根据条件查询集合
     */
    public List<Organiza> getOrganizas(Organiza organiza)
    {
        List<Organiza> orgList = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter OrganizaDaoImpl getOrganizas() method");
        }
        orgList = new ArrayList<Organiza>();

        // 执行变更信息
        orgList = getSqlMapClientTemplate().queryForList(GET_ORG_LIST, organiza);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit OrganizaDaoImpl getOrganizas() method");
        }
        return orgList;
    }

    /**
     * 更新
     */
    public void updateOrganiza(Organiza organiza)
    {
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter OrganizaDaoImpl updateOrganiza() method");
        }

        // 执行变更信息
        getSqlMapClientTemplate().update(UPDATE_ORG, organiza);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit OrganizaDaoImpl updateOrganiza() method");
        }
    }

    /**
     * 根据城市获取所有地市下组织架构
     * 
     * @param city
     * @return
     */
    public List<Organiza> getOrganizaByCity(String city)
    {
        List<Organiza> cityOrgList = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter OrganizaDaoImpl getOrganizaByCity() method");
        }

        // 实例化集合对象
        cityOrgList = new ArrayList<Organiza>();

        // 实例化对象
        Organiza org = new Organiza();

        // 添加查询属性
        org.setCity(city);

        // 执行变更信息 获取当前城市下所有组织列表信息
        cityOrgList = getSqlMapClientTemplate().queryForList(GET_ORG_LIST, org);

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit OrganizaDaoImpl getOrganizaByCity() method");
        }
        return cityOrgList;
    }

}
